﻿#include "Preview.h"
#include "../log/SysLog.h"
#include "../util/TextUtil.h"
#include "../util/FileUtil.h"

Preview::Preview()
{
}

Preview::~Preview()
{
	cv::destroyAllWindows();
}

void Preview::Show()
{
	std::unique_lock<std::mutex> _guard(m_lock);
	if (m_queue.size() > 0)
	{
		ImageItemPtr item = m_queue.front();
		m_queue.pop();
		if (item)
		{
			// 图片缩放n倍后显示
			float scale = 1.0;
			cv::Mat& frame = item->image;
			cv::resize(frame, frame, cv::Size(frame.cols * scale, frame.rows * scale));
			cv::imshow(item->window, frame);
			cv::waitKey(1);
		}
	}
}

void Preview::Save(cv::Mat& image)
{
	std::string path = FileUtil::GetAppPath("/images") + TextUtil::get_mutable_instance().GetGuid() + ".jpg";
	cv::imwrite(path, image);
}

void Preview::Push(std::string window, cv::Mat& image)
{
	std::unique_lock<std::mutex> _guard(m_lock);
	ImageItemPtr item = std::make_shared<IMG_ITEM>();
	item->window = window;
	item->image = image.clone();
	m_queue.push(item);
}

void Preview::Test()
{
	float scale = 0.8;
	cv::Mat frame = cv::imread("d:/test.jpg");
	int type = frame.type();
	cv::resize(frame, frame, cv::Size(frame.cols * scale, frame.rows * scale));
	cv::namedWindow("11111111111111111111111111");
	cv::imshow("11111111111111111111111111", frame);
	cv::waitKey(1);
}